package controllers;
/**
 * @author Wojtek N., Kamil M.
 * @version 0.7
 * Class which initiate client and control its movements.
 */
import java.io.IOException;
import java.net.UnknownHostException;

import models.PlayerInfo;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.allin;
import views.html.bet;
import views.html.call;
import views.html.changeCards;
import views.html.chat;
import views.html.check;
import views.html.fold;
import views.html.index;
import views.html.join;
import views.html.main;
import views.html.mainView;
import views.html.next;
import views.html.raise;
import views.html.waiting;

public class Application extends Controller
{
	private static PlayersPack players = new PlayersPack();
	private static ServerComPack communicators = new ServerComPack();
	private static MsgPack messages = new MsgPack();
	private static int allPlayers = 0;
	
	private static final String HOST = "127.0.0.1";
	private static final int PORT = 6789;
	
	private static void print(String msg)
	{
		System.out.println(msg);
	}
	
	/**
	 * Main site (first what occurs to the user).
	 * @return Welcome page
	 */
	public static Result index()
	{
		return ok(index.render("Welcome to Play app of Badugi Poker! "
				+ "Click button below to start the game. :)"));
	}
	
	/**
	 * Application action after using Join button.
	 * @return
	 */
	public static Result join()
	{
		if(session("allPlayers") != null)
		{
			return ok(index.render("We are so sorry, but you are "
					+ "trying to enter the game in which you already are, "
					+ "or server you are trying to connect is offline."));
		}
		try
		{
			ClientServerCom tempCom = communicators.newServerCom(allPlayers);
			tempCom.connect(HOST, PORT);
			PlayerInfo tempPlayer = players.newPlayer(allPlayers);
			Messages tempMsg = messages.newMsg(allPlayers);
			tempPlayer.setAction(tempCom.checkAction());
			tempCom.getWhatIsMine();
			if(tempPlayer.getAction().equals("INI"))
			{
				tempPlayer.setStartingParam(tempCom.getMsgFromServer());
				print("Your ID: " + tempPlayer.getMyID());
				session("allPlayers", Integer.toString(allPlayers));
			}
			tempPlayer.setInfo(tempCom.display());
			tempMsg.addMessage(tempPlayer.getInfo());
			allPlayers++;
			return redirect(routes.Application.waiting());
			
		}
		catch(Exception e)
		{
			return ok(index.render("Failed to connect. "
					+ "Probably server is down."
					+ "Try again later."));
		}
	}
	
	/**
	 * While successfully connected, but not all players are yet in the game.
	 * @return
	 */
	public static Result waiting()
	{
		if(session("allPlayers") == null)
		{
			return redirect(routes.Application.index());
		}
		return ok(waiting.render("Please wait for other to connect. :)"));
	}
	
	public static Result start()
	{
		if(session("allPlayers") == null)
		{
			return redirect(routes.Application.index());
		}
		int nr = Integer.parseInt(session("allPlayers"));
		ClientServerCom tempCom = communicators.getServerCom(nr);
		PlayerInfo tempPlayer = players.getPlayer(nr);
		Messages tempMsg = messages.getMsg(nr);
		if(tempPlayer.isStartButton() == false)
		{
			return ok(waiting.render("Please wait for other to connect. :)"));
		}
		else
		{
			tempCom.sendMessage(tempPlayer.getMyID() + "RDY__Y");
			tempMsg.addMessage("Waiting for other players");
			return redirect(routes.Application.mainView());
		}
	}
	
	public static Result mainView()
	{
		if(session("allPlayers") == null)
		{
			return redirect(routes.Application.index());
		}
		return ok(mainView.render("Game has begun."));
	}
	
	public static Result allin()
	{
		int nr = Integer.parseInt(session("allPlayers"));
		ClientServerCom tempCom = communicators.getServerCom(nr);
		PlayerInfo tempPlayer = players.getPlayer(nr);
		Messages tempMsg = messages.getMsg(nr);
		if(tempPlayer.isBettingRound() == true)
		{
			if(tempPlayer.isAllInButton() == true)
			{
				tempPlayer.setSentToServer(tempPlayer.getMyID()
						+ "AUCal" + tempPlayer.getMyTokens());
				tempCom.sendMessage(tempPlayer.getSentToServer());
				print("All-In: " + tempPlayer.getSentToServer());
				return ok(allin.render("You have played All-In."));
			}
			else
			{
				return ok(allin.render("You cannot play All-In."));
			}
		}
		else
		{
			return ok(allin.render("What are you doing fool! "
					+ "It is not your turn now!"));
		}
	}
	
	public static Result bet(String bett)
	{
		int nr = Integer.parseInt(session("allPlayers"));
		ClientServerCom tempCom = communicators.getServerCom(nr);
		PlayerInfo tempPlayer = players.getPlayer(nr);
		Messages tempMsg = messages.getMsg(nr);
		if(tempPlayer.isBettingRound() == true)
		{
			if(tempPlayer.isBetButton() == true)
			{
				tempPlayer.setSentToServer(tempPlayer.getMyID()
						+ "AUCbe" + bett);
				tempCom.sendMessage(tempPlayer.getSentToServer());
				print("Bet: " + tempPlayer.getSentToServer());
				return ok(bet.render("You have played Bet."));
			}
			else
			{
				return ok(bet.render("You cannot play Bet."));
			}
		}
		else
		{
			return ok(bet.render("What are you doing fool! "
					+ "It is not your turn now!"));
		}
	}
	
	public static Result call()
	{
		int nr = Integer.parseInt(session("allPlayers"));
		ClientServerCom tempCom = communicators.getServerCom(nr);
		PlayerInfo tempPlayer = players.getPlayer(nr);
		Messages tempMsg = messages.getMsg(nr);
		if(tempPlayer.isBettingRound() == true)
		{
			if(tempPlayer.isCallButton() == true)
			{
				tempPlayer.setSentToServer(tempPlayer.getMyID()
						+ "AUCca" + tempPlayer.getCall());
				tempCom.sendMessage(tempPlayer.getSentToServer());
				print("Call: " + tempPlayer.getSentToServer());
				return ok(call.render("You have played Call."));
			}
			else
			{
				return ok(call.render("You cannot play Call."));
			}
		}
		else
		{
			return ok(call.render("What are you doing fool! "
					+ "It is not your turn now!"));
		}
	}
	
	public static Result check()
	{
		int nr = Integer.parseInt(session("allPlayers"));
		ClientServerCom tempCom = communicators.getServerCom(nr);
		PlayerInfo tempPlayer = players.getPlayer(nr);
		Messages tempMsg = messages.getMsg(nr);
		if(tempPlayer.isBettingRound() == true)
		{
			if(tempPlayer.isCheckButton() == true)
			{
				tempPlayer.setSentToServer(tempPlayer.getMyID()
						+ "AUCch_");
				tempCom.sendMessage(tempPlayer.getSentToServer());
				print("Check: " + tempPlayer.getSentToServer());
				return ok(check.render("You have played Check."));
			}
			else
			{
				return ok(check.render("You cannot play Check."));
			}
		}
		else
		{
			return ok(check.render("What are you doing fool! "
					+ "It is not your turn now!"));
		}
	}
	
	public static Result fold()
	{
		int nr = Integer.parseInt(session("allPlayers"));
		ClientServerCom tempCom = communicators.getServerCom(nr);
		PlayerInfo tempPlayer = players.getPlayer(nr);
		Messages tempMsg = messages.getMsg(nr);
		if(tempPlayer.isBettingRound() == true)
		{
			tempPlayer.setSentToServer(tempPlayer.getMyID()
					+ "AUCfo_");
			tempCom.sendMessage(tempPlayer.getSentToServer());
			print("All-In: " + tempPlayer.getSentToServer());
			return ok(fold.render("You have played Fold."));
		}
		else
		{
			return ok(fold.render("What are you doing fool! "
					+ "It is not your turn now!"));
		}
	}
	
	public static Result raise(String raisee)
	{
		int nr = Integer.parseInt(session("allPlayers"));
		ClientServerCom tempCom = communicators.getServerCom(nr);
		PlayerInfo tempPlayer = players.getPlayer(nr);
		Messages tempMsg = messages.getMsg(nr);
		if(tempPlayer.isBettingRound() == true)
		{
			if(tempPlayer.isRaiseButton() == true)
			{
				tempPlayer.setSentToServer(tempPlayer.getMyID()
						+ "AUCra" + raisee);
				tempCom.sendMessage(tempPlayer.getSentToServer());
				print("Raise: " + tempPlayer.getSentToServer());
				return ok(raise.render("You have played Raise."));
			}
			else
			{
				return ok(raise.render("You cannot play Raise."));
			}
		}
		else
		{
			return ok(raise.render("What are you doing fool! "
					+ "It is not your turn now!"));
		}
	}
	
	public static Result changeCards(String cards)
	{
		int nr = Integer.parseInt(session("allPlayers"));
		ClientServerCom tempCom = communicators.getServerCom(nr);
		PlayerInfo tempPlayer = players.getPlayer(nr);
		Messages tempMsg = messages.getMsg(nr);
		if(tempPlayer.isChangeButton() == true)
		{
			tempPlayer.setSentToServer(tempPlayer.getMyID() + "CHG__"
					+ cards.toUpperCase());
			tempCom.sendMessage(tempPlayer.getSentToServer());
			print("Exchange: "+tempPlayer.getSentToServer());
			return ok(changeCards.render("You have changed cards."));
		}
		else
		{
			return ok(changeCards.render("What are you doing fool! "
					+ "It is not your turn now!"));
		}
	}
	
	public static Result next()
	{
		int nr = Integer.parseInt(session("allPlayers"));
		ClientServerCom tempCom = communicators.getServerCom(nr);
		PlayerInfo tempPlayer = players.getPlayer(nr);
		Messages tempMsg = messages.getMsg(nr);
		if(tempPlayer.isNextButton() == true)
		{
			tempPlayer.defaultState();
			tempCom.sendMessage(tempPlayer.getMyID()+"NXT__Y");
			print(tempPlayer.getMyID()+"NXT__Y");
			return ok(next.render("You have decided to continue."
					+ "Wiat for other players. :)"));
		}
		else
		{
			return ok(next.render("What are you doing fool! "
					+ "Match is not over yet!"
					+ "Wait for the end of the current game!"));
		}
	}
	
	public static Result chat()
	{
		if(session("allPlayers") == null)
		{
			return redirect(routes.Application.index());
		}
		int nr = Integer.parseInt(session("allPlayers"));
		Messages msg = messages.getMsg(nr);
		return ok(chat.render(msg.merge()));
	}
	
	
	/**
	 * Method in which, depending on income from server, different actions are taken.
	 * @param inFromServer an income from server
	 */
	public static void messageReceived(int nr, String inFromServer)
	{
		ClientServerCom com = communicators.getServerCom(nr);
		PlayerInfo p = players.getPlayer(nr);
		Messages msg = messages.getMsg(nr);
		try
		{
			com.getWhatIsMine();

			switch(com.checkAction(inFromServer))
			{
				// Are you ready to start?
				case "RDY":
					ready(com, p, msg, inFromServer);
					break;
				// Starting the game - have your cards in here.
				case "BEG":
					com.printClient(inFromServer);
					p.setHand(com.getMsgFromServer());
					print("Cards have come: "+p.getHandForView());
					msg.addMessage("Cards have come: "+p.getHandForView());
					break;
				// Remember who have dealer button, big blind and small blind.
				case "DBS":
					setDBS(com, p, msg, inFromServer);
					break;
				// Auction phase - here you can do an action or receive what others do.
				case "AUC":
					auctionPhase(com, p, msg, inFromServer);
					break;
				// It's time for changing cards phase.
				case "CHG":
					cardChangePhase(com, p, msg, inFromServer);
					break;
				// Oh, so we have a winner. Let's check who it is.
				case "WON":
					winnerSelectPhase(com, p, msg, inFromServer);
					break;
				// Asked if I want to continue.
				case "NXT":
					askContinue(com, p, msg, inFromServer);
					break;
				case "ERR":
					com.printClient(inFromServer.substring(6));
					msg.addMessage("Error: " + inFromServer.substring(6));
					break;
			}
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
	}

	private static void askContinue(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		com.printClient("Do you wish to continue?");
		switch(inFromServer.substring(6, 8))
		{
			// Do I wish to continue?
			case "YN":
				p.setNextButtonUp();
				msg.addMessage("Do you want fight again young Jedi ?");
				break;
			// Damn, someone have lost... We need to start another table.
			case "PR":
				p.setNextButtonDown();
				p.setLooserID(inFromServer.substring(8));
				msg.addMessage("Broken is player " + p.getLooserID() + ". "
						+ "End we need due to him.");
				break;
			// Game have ended. Don't worry, you will be transfered automaticlly to new table.
			case "ED":
				p.setNextButtonDown();
				msg.addMessage("Dark side in others I feel, need we end."
						+ "Untill next time we will see.");
				break;
		}
	}

	private static void winnerSelectPhase(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		com.printClient("Ladies and gentleman, and the winner is...");
		switch(inFromServer.substring(4, 6))
		{
			// We've got one winner! Good. If you are this one, change your data.
			case "on":
				oneWinner(com, p, msg, inFromServer);
				break;
			// We've got a draw! Check if you are on the list of winners, maybe you have won something.
			case "dr":
				draw(com, p, msg, inFromServer);
				break;	
		}
	}

	private static void draw(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		p.setHowManyWinners(inFromServer.split("\\|")[0].substring(6));
		p.setWinnersID(inFromServer.split("\\|")[0].substring(6));
		p.setWinningAmount(inFromServer.split("\\|")[1]);
		String sepWinnersID = "";
		for(int i=0; i<p.getHowManyWinners(); i++)
		{
			String temp = p.getWinnersID().substring(i, i+1);
			if(temp.equals(p.getMyID()))
			{
				p.setMyTokensAfterAll(Integer.parseInt(p.getWinningAmount()));
			}
			else
			{
				p.setMyTokensAfterAll(0);
			}
			sepWinnersID += temp + " ";
		}
		com.printClient("It was a draw between " +sepWinnersID+"! "
				+ "Each of them won " + p.getWinningAmount()+" tokens! "
						+ "Congrats!");
		msg.addMessage("It was a draw between " +sepWinnersID+"! "
				+ "Each of them won " + p.getWinningAmount()+" tokens! "
						+ "Congrats!");
	}

	private static void oneWinner(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		p.setHowManyWinners(inFromServer.split("\\|")[0].substring(6));
		p.setWinnerID(inFromServer.split("\\|")[0].substring(6));
		p.setWinningAmount(inFromServer.split("\\|")[1]);
		if(p.getWinnerID().equals(p.getMyID()))
		{
			p.setMyTokensAfterAll(Integer.parseInt(p.getWinningAmount()));
		}
		else
		{
			p.setMyTokensAfterAll(0);
		}
		com.printClient("Player " + p.getWinnerID()+"! "
				+ "Congrats! You won " + p.getWinningAmount() + "tokens!");
		msg.addMessage("Player " + p.getWinnerID()+"! "
				+ "Congrats! You won " + p.getWinningAmount() + "tokens!");
	}

	private static void cardChangePhase(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		com.printClient("Card exchange phase.");
		switch(inFromServer.substring(4,6))
		{
			// It is not your turn. You should wait.
			case "__":
				com.printClient("Wait for your turn. Other players exchanging cards...");
				msg.addMessage("Wait for your turn. Other players exchanging cards...");
				break;
			// OK, so it's your turn to exchange cards.
			case "os":
				com.printClient("Choose wisely, young padawan, which cards exchange you will...");
				msg.addMessage("Choose wisely, young padawan, which cards exchange you will...");
				p.setChangeButtonUp();
				break;
			// Here you go. Your new hand has come.
			case "cd":
				if(p.getMyID().equals(inFromServer.substring(6,7)))
				{
					p.setHand(inFromServer);
				}
				com.printClient("Your current hand: "+p.getHandForView());
				msg.addMessage("Cards have come: "+p.getHandForView());
				p.setChangeButtonDown();
			// I'm just working here.
			default:
				p.setChangeButtonDown();
				p.setPlayersCardChange(inFromServer);
				com.printClient("Card change phase has ended with: " + p.getPlayersCardChange());
				msg.addMessage("Card change phase has ended with: " + p.getPlayersCardChange());
				break;
		}
	}

	private static void auctionPhase(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		com.printClient("We are currently in " + inFromServer.substring(6, 7) + " round.");
		switch(inFromServer.substring(4,6))
		{
			// Welcome in x bet round!
			case "__":
				p.setBetRound(inFromServer.substring(6, 7));
				msg.addMessage("Current bet round: " + inFromServer.substring(6, 7));
				break;
			// Hey! Don't sleep, it's your turn. What will you do?
			case "ur":
				msg.addMessage("Your time has come. What will you do?");
				p.enableAll();
				break;
			// Someone has played 'check'. Check if it was you. If yes, than change your data.
			case "ch":
				playedCheck(com, p, msg, inFromServer);
				break;
			// Someone has played 'bet'. Check if it was you. If yes, than change your data.
			case "be":
				playedBet(com, p, msg, inFromServer);
				break;
			// Someone has played 'raise'. Check if it was you. If yes, than change your data.
			case "ra":
				playedRaise(com, p, msg, inFromServer);
				break;
			// Someone has played 'call'. Check if it was you. If yes, than change your data.
			case "ca":
				playedCall(com, p, msg, inFromServer);
				break;
			// Someone has played 'fold'. Check if it was you. If yes, than change your data.
			case "fo":
				playedFold(com, p, msg, inFromServer);
				break;
			// Someone has played 'all-in'. Check if it was you. If yes, than change your data.
			case "al":
				playedAllIn(com, p, msg, inFromServer);
				break;
		}
	}

	private static void playedAllIn(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		p.setPlayersID(inFromServer.substring(7, 8));
		p.setPlayersBet(inFromServer.substring(8));
		if(p.getMyID().equals(p.getPlayersID()))
		{
			p.setMyBetInt(Integer.toString(p.getPlayersBet() + p.getMyBetInt()));
			p.setMyTokens(0);
			p.disableAll();
			com.printClient("All-In.");
			msg.addMessage("All-In. Your current bankroll is: " + p.getMyTokens());
		}
		else
		{
			com.printClient("Player " + p.getPlayersID() + ": All-In.");
			msg.addMessage("Player " + p.getPlayersID() + ": All-In.");
		}
	}

	private static void playedFold(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		p.setPlayersID(inFromServer.substring(7, 8));
		if(p.getMyID().equals(p.getPlayersID()))
		{
			p.disableAll();
			com.printClient("Fold.");
			msg.addMessage("Fold.");
		}
		else
		{
			com.printClient("Player " + p.getPlayersID() + ": Fold.");
			msg.addMessage("Player " + p.getPlayersID() + ": Fold.");
		}
	}

	private static void playedCall(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		p.setPlayersID(inFromServer.substring(7, 8));
		p.setPlayersBet(inFromServer.split("\\|")[1]);
		p.setPlayersTokens(inFromServer.split("\\|")[0].substring(8));
		if(p.getMyID().equals(p.getPlayersID()))
		{
			p.setMyBetInt(Integer.toString(p.getCurrentTopBet()));
			p.setMyTokens(Integer.parseInt(p.getPlayersTokens()));
			p.disableAll();
			com.printClient("Call.");
			msg.addMessage("Call. Your current bankroll is: " + p.getMyTokens());
		}
		else
		{
			com.printClient("Player " + p.getPlayersID() + ": Call.");
			msg.addMessage("Player " + p.getPlayersID() + ": Call.");
		}
	}

	private static void playedRaise(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		p.setPlayersID(inFromServer.substring(7, 8));
		p.setPlayersBet(inFromServer.split("\\|")[1]);
		p.setPlayersTokens(inFromServer.split("\\|")[0].substring(8));
		p.setRaised(true);
		if(p.getSmallID().equals(p.getPlayersID()))
		{
			p.setCurrentTopBet(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getSmallBlind())));
		}
		else if(p.getBigID().equals(p.getMyID()))
		{
			p.setCurrentTopBet(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getBigBlind())));
		}
		else
		{
			p.setCurrentTopBet(Integer.toString(p.getPlayersBet()));
		}
		com.printClient("Current max bet: " + p.getCurrentTopBet()+" in round "+p.getBetRound());
		if (p.getMyID().equals(p.getPlayersID()))
		{
			if(p.getSmallID().equals(p.getMyID()))
			{
				p.setMyBetInt(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getSmallBlind())));
			}
			else if(p.getBigID().equals(p.getMyID()))
			{
				p.setMyBetInt(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getBigBlind())));
			}
			else
			{
				p.setMyBetInt(Integer.toString(p.getMyBetInt()));
			}
			p.setMyTokens(Integer.parseInt(p.getPlayersTokens()));
			p.disableAll();
			com.printClient("Raise.");
			msg.addMessage("Raise. Your current bankroll is: " + p.getMyTokens());
		}
		else
		{
			com.printClient("Player " + p.getPlayersID() + ": Raise.");
			msg.addMessage("Player " + p.getPlayersID() + ": Raise.");
		}
		msg.addMessage("Current max bet: " +p.getCurrentTopBet());
	}

	private static void playedBet(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		p.setPlayersID(inFromServer.substring(7, 8));
		p.setPlayersBet(inFromServer.split("\\|")[1]);
		p.setPlayersTokens(inFromServer.split("\\|")[0].substring(8));
		p.setBetted(true);
		if(p.getSmallID().equals(p.getPlayersID()))
		{
			com.printClient("\n"+Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getSmallBlind())));
			p.setCurrentTopBet(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getSmallBlind())));
		}
		else if(p.getBigID().equals(p.getMyID()))
		{
			com.printClient("\n"+Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getBigBlind())));
			p.setCurrentTopBet(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getBigBlind())));
		}
		else
		{
			p.setCurrentTopBet(Integer.toString(p.getPlayersBet()));
		}
		com.printClient("Current max bet: " + p.getCurrentTopBet()+" in round "+p.getBetRound());
		if(p.getMyID().equals(p.getPlayersID()))
		{
			if(p.getSmallID().equals(p.getMyID()))
			{
				p.setMyBetInt(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getSmallBlind())));
			}
			else if(p.getBigID().equals(p.getMyID()))
			{
				p.setMyBetInt(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getBigBlind())));
			}
			else
			{
				p.setMyBetInt(Integer.toString(p.getMyBetInt()));
			}
			p.setMyTokens(Integer.parseInt(p.getPlayersTokens()));
			p.disableAll();
			com.printClient("Bet.");
			msg.addMessage("Bet. Your current bankroll is: " + p.getMyTokens());
		}
		else
		{
			com.printClient("Player " + p.getPlayersID() + ": Bet.");
			msg.addMessage("Player " + p.getPlayersID() + ": Bet.");
		}
		msg.addMessage("Current max bet: " +p.getCurrentTopBet());
	}

	private static void playedCheck(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		p.setPlayersID(inFromServer.substring(7));
		if(p.getMyID().equals(p.getPlayersID()))
		{
			p.disableAll();
			com.printClient("Check.");
			msg.addMessage("Check.");
		}
		else
		{
			com.printClient("Player " + p.getPlayersID() + ": Check.");
			msg.addMessage("Player " + p.getPlayersID() + ": Check.");
		}
	}

	private static void setDBS(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		com.printClient("Dealer, big blind, small blind holder as follows: "+inFromServer.substring(6));
		p.setDealerID(inFromServer.substring(6,7));
		p.setSmallID(inFromServer.substring(8, 9));
		p.setBigID(inFromServer.substring(10, 11));
		p.setCurrentTopBet(p.getBigBlind());
		if(p.getDealerID().equals(p.getMyID()))
		{
			msg.addMessage("You have dealer button. "
					+ "Your current bet is: 0. "
					+ "Your current bankroll is: "
					+ p.getMyTokens());
		}
		else if(p.getBigID().equals(p.getMyID()))
		{
			msg.addMessage("You have big blind."
					+ "Your current bet is: "
					+ p.getMyBet()
					+ ". Your current bankroll is: "
					+ p.getMyTokens());
		}
		else if(p.getSmallID().equals(p.getMyID()))
		{
			msg.addMessage("You have small blind."
					+ "Your current bet is: "
					+ p.getMyBet()
					+ ". Your current bankroll is: "
					+ p.getMyTokens());
		}
		msg.addMessage("Player " + p.getDealerID() 
				+ " has dealer button. Player " + p.getSmallID() 
				+ " has small blind. Player " + p.getBigID() 
				+ " has big blind. Current max bet is: "
				+ p.getCurrentTopBet());
	}

	private static void ready(ClientServerCom com, PlayerInfo p, Messages msg, String inFromServer) 
	{
		if(inFromServer.substring(6,7).equals("?"))
		{
			p.setStartUp();
			print(inFromServer);
		}
		else
		{
			msg.addMessage("Player " 
					+ inFromServer.substring(6,7)
					+ "has joined the game. Table is prepered for"
					+ inFromServer.substring(7, 8));
		}
	}
	
	public static void connectionException() 
	{
		System.err.println("Thread interrupted.");

	}

	public static void nullPointerException() 
	{
		System.err.println("Connection Failure - NullPointerException from Thread occured.");
	}
	
	
}
